//
//  PhysicalRecordController.m
//  HarmonizePub
//
//  Created by rzhy on 2017/1/21.
//  Copyright © 2017年 Zy. All rights reserved.
//

#import "PhysicalRecordController.h"
#import "Config.h"
#import <YYModel.h>
#import "NSString+Zy.h"
#import "WebViewController.h"
#import "PhysicalRecordHeadView.h"
#import "PhysicalRecordCell.h"
#import "DepTableView.h"
#import "Physical.h"

static NSString *CellIdentifier = @"Cell";

@interface PhysicalRecordController () <UITableViewDataSource, UITableViewDelegate, DepTableViewDelegate>
{
    NSArray *_dataArry;
    NSArray *_headTitleArray;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation PhysicalRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    XHLog(@"data: %@", self.dataDictionary);
    self.title = @"总检报告";
    _headTitleArray = @[
                        @{@"image": @"bbzs.png", @"text" : @"报告综述:"},
                        @{@"image": @"zjjy.png", @"text" : @"总检建议:"},
                        @{@"image": @"ksxj.png", @"text" : @"科室小结:"}
                        ];
    _dataArry = self.dataDictionary[@"list"];
    UINib *nib = [UINib nibWithNibName:[PhysicalRecordCell registerName] bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableView delegate/dataSource Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _headTitleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        NSString *zs = self.dataDictionary[@"zs"];
        return [PhysicalRecordCell CellHeightWithText:zs];
    } else if (indexPath.section == 1) {
        NSString *jy = self.dataDictionary[@"jy"];
        return [PhysicalRecordCell CellHeightWithText:jy];
    } else {
        CGFloat height = [DepTableView tableViewHeightWithArray:_dataArry] + 8.0f; 
        return height;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PhysicalRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (indexPath.section == 0) {
        // 报告综述
        NSString *text = self.dataDictionary[@"zs"];
        [cell setCellText:text];
    } else if (indexPath.section == 1) {
        // 总检建议
        NSString *text = self.dataDictionary[@"jy"];
        [cell setCellText:text];
    } else {
        // 科室小结
        DepTableView *tableView = [[DepTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.tableDelegate = self;
        tableView.dataArray = _dataArry;
        [cell addSubview:tableView];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    PhysicalRecordHeadView *headView = [PhysicalRecordHeadView headView];
    [headView setDataDictionary:_headTitleArray[section]];
    return headView;
}

- (void)tableView:(DepTableView *)tableView didSelectRowAtPhysical:(Physical *)physical {
    NSString *ksdm = physical.ksdm;
    NSString *urlString = [NSString stringWithFormat:@"%@?ksdm=%@&tjbh=%@", XHTjbgksmx, ksdm, self.tjbh];
    WebViewController *webView = [[WebViewController alloc] init];
    webView.title = physical.ksmc;
    webView.url = [URLTool webURLStringWithPathString:urlString token:[UserDefaultsTool stringForKey:XHToken]];
    [self.navigationController pushViewController:webView animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

@end
